package com.laijiale.Service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.laijiale.mapper.DeptMapper;
import com.laijiale.Service.DeptService;
import com.laijiale.dto.*;
import com.laijiale.entities.Dept;
import com.laijiale.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author laijiale
 * @date 2024年07月28日 12:02
 */
@Service
@CacheConfig(cacheNames = "dept")
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;


@CacheEvict(allEntries = true)
@Override
public int insert(DeptInsertDTO dto) {
    Dept dept = BeanUtil.copyProperties(dto, Dept.class);
    // 设置默认值
    dept.setDeleted(0);
    dept.setCreated(LocalDateTime.now());
    dept.setUpdated(LocalDateTime.now());
    // DB添加
    int result = deptMapper.insert(dept);
    if (result <= 0) {
        throw new ServiceException("DB添加失败");
    }
    return result;
}
    @Cacheable(key = "#id", condition = "#id !=null", unless = " #result==null")
    @Override
    public Dept selectById(Long id) {
        Dept result = deptMapper.selectById(id);
        if (ObjectUtil.isNull(result)) {
            throw new ServiceException("ID不存在");
        }
        return result;
    }
    @CacheEvict(allEntries = true)
    @Override
    public int updateById(DeptUpdateDTO dto) {
        Dept dept = BeanUtil.copyProperties(dto, Dept.class);
        // 设置默认值
        dept.setCreated(null);
        dept.setUpdated(LocalDateTime.now());
        // DB修改
        int result = deptMapper.updatedById(dept);
        if (result <= 0) {
            throw new ServiceException("DB修改失败");
        }
        return result;
    }
    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteById(Long id) {
        //todo 查询记录是否被引用
        int result = deptMapper.logicDeleteById(id);
        if (result <= 0) {
            throw new ServiceException("DB逻辑删除失败");
        }
        return result;
    }

    @CacheEvict(allEntries = true)
    @Override
    public int logicDeleteByIds(List<Long> ids) {
        //todo 查询记录是否被引用
        int result = deptMapper.logicDeleteByIds(ids);
        if (result <= 0) {
            throw new ServiceException("DB逻辑批删失败");

        }
        return result;
    }
@Cacheable(key = "#root.methodName",unless = "#result==null")
    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }
    @Cacheable(key = "#root.methodName + ':' + " +
    "#pageDTO.getPageNum()+':' + " +
    "#pageDTO.getPageSize()",unless = "#result == null ")
    @Override
    public PageInfo<Dept> page(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());

        List<Dept> list = deptMapper.list();
        PageInfo<Dept> pageInfo = new PageInfo(list);

        return pageInfo;
    }

        @Cacheable(key = "#root.methodName + ':' + " +
                "#dto.getTitle() + ':' + " +
                "#dto.getPageNum() + ':' +" +
                "#dto.getPageSize()",unless = "#result==null")
        @Override
        public PageInfo<Dept> pageLikeTitle(DeptPageDTO dto) {
            PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
            return new PageInfo<>(deptMapper.listLikeTitle(dto.getTitle()));
        }
    }

